package com.lj.graduate.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.graduate.entity.User;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.lj.graduate.service.ICommodityService;
import com.lj.graduate.entity.Commodity;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljy
 * @since 2023-03-24
 */
@RestController
@RequestMapping("/commodity")
        public class CommodityController {
    
@Resource
private ICommodityService commodityService;

// 新增或者更新
@PostMapping
public boolean save(@RequestBody Commodity commodity) {
        return commodityService.saveOrUpdate(commodity);
        }

@DeleteMapping("/{id}")
public Boolean delete(@PathVariable Integer id) {
        return commodityService.removeById(id);
        }

@PostMapping("/del/batch")
public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return commodityService.removeByIds(ids);
        }

@GetMapping
public List<Commodity> findAll() {
        return commodityService.list();
        }

@GetMapping("/{id}")
public Commodity findOne(@PathVariable Integer id) {
        return commodityService.getById(id);
        }

@GetMapping("/page")
public IPage<Commodity> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String commodityName) {
        IPage<Commodity> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        if (!"".equals(commodityName)) {
                queryWrapper.like("commodity_name", commodityName);
        }
//        queryWrapper.orderByDesc("id");
        return commodityService.page(new Page<>(pageNum, pageSize), queryWrapper);
        }

        }

